package com.tan00xu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tan00xu.dao.UserMapper;
import com.tan00xu.pojo.User;
import com.tan00xu.service.TokenService;
import com.tan00xu.service.UserService;
import com.tan00xu.vo.UserVo;
import com.tan00xu.vo.ErrorCode;
import com.tan00xu.vo.LoginUserVo;
import com.tan00xu.vo.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;



    @Autowired
    private TokenService tokenService;


    /**
     * 根据用户id查询用户
     * @param id 用户id
     * @return User
     */
    @Override
    public User getUserById(Long id) {
        User user = userMapper.selectById(id);
        if (user == null) {
            user = new User();
            user.setNickname("游客");
        }
        return user;
    }

    /**
     * 查询用户的id，昵称，头像
     * @param userId 用户的id
     * @return CriticVo 用户信息映射
     */
    @Override
    public UserVo getUserVoByUserId(Long userId) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            user = new User();
            user.setId(0L);
            user.setAvatar("");
            user.setNickname("游客");
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
//        criticVo.setId(user.getId());
//        criticVo.setNickname(user.getNickname());
//        criticVo.setAvatar(user.getAvatar());
        return userVo;
    }

    @Override
    public User getUserByAccountAndPassword(String account, String password) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount,account);
        queryWrapper.eq(User ::getPassword,password );
        queryWrapper.select(User::getAccount,User::getId,User::getAvatar,User::getNickname);
        //查到就停，保证效率
        queryWrapper.last("limit 1");
        return userMapper.selectOne(queryWrapper);
    }

    /**
     * 根据token在redis缓存中查询用户信息
     * @param token 前端传回的token
     * @return Result
     */
    @Override
    public Result getUserByToken(String token) {
        /*
           1. 验证token是否合法
                 验证token是否过期，是否为空，redis中是否存在
           2. 如果校验失败，返回错误信息
           3. 如果校验成功，返回用户信息 LoginUserVo
         */
        User user = tokenService.checkToken(token);
//        CmdOutputInformationUtils.error("用户信息" + user);
        if (user == null) {
            return Result.fail(ErrorCode.TOKEN_ILLEGAL.getCode(),ErrorCode.TOKEN_ILLEGAL.getMsg());
        }
        //把user对象转换成LoginUserVo对象，并返回

        LoginUserVo loginUserVo = new LoginUserVo();
//        loginUserVo.setId(user.getId());
//        loginUserVo.setNickname(user.getNickname());
//        loginUserVo.setAvatar(user.getAvatar());
//        loginUserVo.setAccount(user.getAccount());
        BeanUtils.copyProperties(user, loginUserVo);
        return Result.success(loginUserVo);

    }

    @Override
    public User getUserByAccount(String account) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getAccount,account);
        queryWrapper.last("limit 1");
        return this.userMapper.selectOne(queryWrapper);
    }

    @Override
    public void save(User user) {
        /*
            id自增
            默认的是分布式的自增长，使用雪花算法
            mybatis-plus
         */
        this.userMapper.insert(user);

    }
}

